package com.cogent.dynamicform.form.impl;

import java.util.HashMap;

import com.cogent.common.util.MapUtil;
import com.cogent.common.util.StringUtil;
import com.cogent.dynamicform.domain.BaseFormColumnDomain;
import com.cogent.dynamicform.domain.FormColumnDomain;
import com.cogent.dynamicform.form.AbstractDynamicForm;
import com.cogent.expression.facade.ExpressionFacade;

public class HiddenDynamicFormImpl extends AbstractDynamicForm {
	public String genQueryField(BaseFormColumnDomain formColumnDomain, HashMap formMap) {
		StringBuffer str = new StringBuffer();
		String name = formColumnDomain.getName();
		String curvalue = ExpressionFacade.getExpressionValue(formMap, formColumnDomain.getCalformula());
		if(StringUtil.isEmpty(curvalue))
			curvalue = MapUtil.getStringValue(formMap, formColumnDomain.getName(), ExpressionFacade.getExpressionValue(formMap, formColumnDomain.getExpression()));
		str.append("<input type=\"hidden\" name=\"").append(name).append("\" id=\"").append(name)
			.append("\" value=\"").append(curvalue)
			.append("\"/>");
		
		return str.toString();
	}
	
	public String genInputField(FormColumnDomain formColumnDomain, HashMap formMap) {
		String typecode = formColumnDomain.getTypecode();
		if(!StringUtil.isEmpty(formColumnDomain.getOpttype()))
			typecode = "";
		String[] temps = typecode.split("_");
		StringBuffer str = new StringBuffer();
		String name = formColumnDomain.getName();
		if(!temps[0].equals("0")) {
			if(temps.length > 1)
				name = temps[1];
		}
		String value = ExpressionFacade.getExpressionValue(formMap, formColumnDomain.getCalformula());
		if(StringUtil.isEmpty(value))
			value = MapUtil.getStringValue(formMap, formColumnDomain.getName());
		if(StringUtil.isEmpty(value))
			value = ExpressionFacade.getExpressionValue(formMap, formColumnDomain.getExpression());
		if(StringUtil.isEmpty(value))
			value = formColumnDomain.getDefaultvalue();
		if(!temps[0].equals("1"))
			str.append("<input type=\"hidden\" name=\"").append(name).append("\" id=\"").append(name)
				.append("\" value=\"").append(value)
				.append("\"/>");
		
		return str.toString();
	}
	
	public String getShowValue(BaseFormColumnDomain formColumnDomain, HashMap formMap) {
		if(formColumnDomain.getShowtype() == 1)
			return "&nbsp;";
		String typecode = formColumnDomain.getTypecode();
		String[] temps = typecode.split("_");
		String ret = MapUtil.getStringValue(formMap, formColumnDomain.getName());
		if(StringUtil.isEmpty(ret))
			ret = "&nbsp;";
		return ret;
	}
}
